package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.IEmployeeService;
import com.itheima.reggie.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@ServletComponentScan
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        //用户名为唯一，调用getone方法
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            //用户名错误，返回错误信息并给出提示
            return R.error("用户名错误，请重新输入");
        }
        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            //密码错误，返回错误信息并给出提示
            return R.error("密码错误，请重新输入");
        }
        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() != 1) {
            //登录状态为非一的其他数，则返回错误信息并给出提示
            return R.error("账号异常，请联系管理员");
        }
        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public R<String> loginOut(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<Employee> addEmp(@RequestBody Employee employee, HttpSession session) {
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser((Long) session.getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success(null);
    }

    /**
     * 分页查询员工信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageEmp(Integer page, Integer pageSize, String name) {
        Page Ipage = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && name != "", Employee::getName, name);
        queryWrapper.groupBy(Employee::getUpdateTime);
        employeeService.page(Ipage, queryWrapper);
        return R.success(Ipage);
    }

    /**
     * 根据id更改员工账号状态
     * @param
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {

        /*Long empId = (Long) session.getAttribute("employee");
        //更新账号时间
        employee.setUpdateTime(LocalDateTime.now());
        //更新账号用户id
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);
        return R.success("更新成功");
    }


    /**
     * 根据id从查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById( @PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null) {
            return R.success(employee);
        }
        return R.error("没有查询到，请输入正确的id");
    }
}
